﻿using AutoMapper;
using GiaSu.Bussiness.Business;
using GiaSu.DataAccess.Entity;
using GiaSu.WebApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GiaSu.WebApplication.Controllers
{
    public class ParentController : Controller
    {
        private readonly IParentBusiness parentBusiness;
        private readonly IGradeBusiness gradeBusiness;
        private readonly ISubjectBusiness subjectBusiness;

        public ParentController(IParentBusiness parentBusiness, IGradeBusiness gradeBusiness, ISubjectBusiness subjectBusiness)
        {
            this.parentBusiness = parentBusiness;
            this.gradeBusiness = gradeBusiness;
            this.subjectBusiness = subjectBusiness;
        }
        // GET: Parent
        public ActionResult Index()
        {
            var listParent = this.parentBusiness.GetAll().ToList();
            return View("Index", Mapper.Map<List<Parent>, List<ParentView>>(listParent));
        }

        // GET: Parent/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Parent/Create
        public ActionResult Add()
        {
            var parent = new ParentAdd
            {
                ListGrade = gradeBusiness.GetAll().Select(grade => new SelectListItem { Text = grade.Name, Value = grade.Id.ToString() }).ToList(),
                ListSubject = subjectBusiness.GetAll().Select(subject => new SelectListItem { Text = subject.Name, Value = subject.Id.ToString() }).ToList()
            };
            return View(parent);
        }

        // POST: Parent/Create
        [HttpPost]
        public ActionResult Add(ParentAdd parentAdd)
        {

            if (ModelState.IsValid)
            {
                var parentDTO = Mapper.Map<ParentAdd, Parent>(parentAdd);
                //parentDTO.Grade = gradeBusiness.GetById(parentDTO.GradeId);
                //parentDTO.Subject = subjectBusiness.GetById(parentDTO.SubjectId);
                this.parentBusiness.Add(parentDTO);
            }
            return RedirectToAction("Index");

        }

        // GET: Parent/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Parent/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Parent/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Parent/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
